<template>
  <el-dialog :title="title" :visible.sync="isShow" width="60%" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-row>
        <el-col :span="8">
          <el-form-item label="公司名称" :label-width="formLabelWidth">
            <el-input v-model="companyName" :disabled="true" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属部门" :label-width="formLabelWidth">
            <!-- <el-input v-model="form.roleName" autocomplete="off"></el-input> -->
            <user-org-info :companyId="companyId" :value="form.orgId" @setOrgData="changeOrgInfo" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属岗位" :label-width="formLabelWidth">
            <el-select v-model="form.postId" placeholder="请选择">
              <el-option v-for="item in postList" :key="item.id" :label="item.postName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户账号" :label-width="formLabelWidth">
            <el-input v-model="form.userLoginName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号码" :label-width="formLabelWidth">
            <el-input v-model="form.phoneNumber" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户性别" :label-width="formLabelWidth">
            <el-radio-group v-model="form.sex">
              <el-radio label="0">男</el-radio>
              <el-radio label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户状态" :label-width="formLabelWidth">
            <el-select v-model="form.status" placeholder="请选择">
              <el-option v-for="item in statusData" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
        
<script>
import UserOrgInfo from './userorginfo.vue'
import { getCompanyDetail } from '@/api/company'
import { getPostListByOrgId } from '@/api/post'
import { createUser, updateUser } from '@/api/user'
export default {
  components: { UserOrgInfo },
  name: 'UserInfoComp',
  data () {
    return {
      form: {
        companyId: '',
        orgId: '',
        postId: '',
        nickName: '',
        userLoginName: '',
        email: '',
        phoneNumber: '',
        sex: '0',
        status: '1'
      },
      formLabelWidth: '100px',
      title: '新增用户',
      statusData: [{
        value: '1',
        label: '正常'
      }, {
        value: '2',
        label: '禁用'
      }, {
        value: '3',
        label: '锁定'
      }],
      postList: [],
      companyName: ""
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    companyId: {
      type: String,
      require: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.orgId = ""
        this.form.postId = ""
        this.form.nickName = ""
        this.form.userLoginName = ""
        this.form.email = ""
        this.form.phoneNumber = ""
        this.form.sex = "0"
        this.form.status = "1"
        this.title = '新增用户'
      } else {
        this.title = '编辑用户'
        this.form = { ...this.item }
        const postList = this.item.postList
        if (postList.length > 0) {
          this.form.postId = postList[0].id
        }
        this.getPostList()
      }
    },
    getCompanyDetail () {
      const param = { companyId: this.companyId }
      getCompanyDetail(param).then((res) => {
        this.companyName = res.data.companyName
      });
    },
    changeOrgInfo (data) {
      this.form.orgId = data
      this.getPostList()
    },
    getPostList () {
      const param = { orgId: this.form.orgId }
      getPostListByOrgId(param).then((res) => {
        this.postList = res.data
      });
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        createUser(param).then((res) => {
          this.result(res)
        });
      } else {
        updateUser(param).then((res) => {
          this.result(res)
        });
      }
    }
  },
  watch: {
    companyId (newValue, oldValue) {
      // 在这里可以执行更多的逻辑
      if (newValue.length > 0 || newValue !== undefined) {
        this.form.companyId = newValue
        this.getCompanyDetail()
      }
    }
  },
}
</script>
    
<style scoped>
</style>
        